#include <iostream>
#include <vector>

using namespace std;
class Solution
{
public:
    string mergeAlternately(string word1, string word2)
    {
        int l1 = word1.length(), l2 = word2.length();
        int i = 0, j = 0;
        string ans;
        while (i < l1 || j < l2)
        {
            if (i < l1)
                ans.push_back(word1[i++]);
            if (j < l2)
                ans.push_back(word2[j++]);
        }
        // ans += l1 > l2 ? word1.substr(l2) : word2.substr(l1);
        return ans;
    }

    string mergeAlternately_2(string word1, string word2)
    {
        int i = 0, j = 0;
        string s = "";
        while (i < (int)word1.size() || j < (int)word2.size())
        {
            if (i < (int)word1.size())
                s += word1[i++];
            if (j < (int)word2.size())
                s += word2[j++];
        }
        return s;
    }
};
int main()
{
    Solution s;
    string word1 = "abcd", word2 = "pq";
    cout << s.mergeAlternately(word1, word2) << endl;
    system("pause");
    return 0;
}
